package com.huarui.part.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.ewaf.framework.exception.BizException;
import org.ewaf.framework.model.ListModel;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.huarui.inventory.model.Inventory;
import com.huarui.part.dao.IPartPropertyDao;
import com.huarui.part.model.PartListModel;
import com.huarui.part.model.PartProperty;
import com.huarui.quotation.error.ErrorCode;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class PartPropertyDao extends BaseDaoImpl implements IPartPropertyDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<PartProperty> getPartCategoryListModel() {

		return this.getSqlMapClientTemplate().queryForList(
				namespace + ".getPartPropertyPage");
	}

	@Override
	public PartProperty getPartPropertyByPartIdAndCompanyId(Integer partId,
			Integer companyId) {
		PartProperty partProperty=new PartProperty();
		partProperty.setPartId(partId);
		partProperty.setCompanyId(companyId);
		return (PartProperty) this.getSqlMapClientTemplate().queryForObject(
				namespace + ".getPartPropertyByPartIdAndCompanyId", partProperty);
	}
	
	public PartProperty getPartPropertyByPartId(Integer partId,Integer companyId,boolean isLock){

		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("partId",partId);
		
		map.put("companyId",companyId);
		
		map.put("isLock", isLock);
		return	(PartProperty)this.getSqlMapClientTemplate().queryForObject(namespace+".getPartPropertyByPartId",map);
	}
	@Override
	public PartProperty getPartProBySourCode(String code, String source) {
		PartProperty result=(PartProperty) this.getSqlMapClientTemplate().queryForObject(namespace+".",code,source);
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public PartProperty getPartPropertyByPartId(Integer partId,Integer companyId) {
		
		Map map = new HashMap();
		
		map.put("partId",partId);
		
		map.put("companyId",companyId);
		
		return	(PartProperty)this.getSqlMapClientTemplate().queryForObject(namespace+".getPartPropertyByPartId",map);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<PartProperty> getPartPropertyListByComplex(String[] params) {
		Map<String, Object> map = new HashMap<String, Object>();
		this.putParams(map, params);
		
		return this.getSqlMapClientTemplate().queryForList(super.namespace + ".getPartProListByComplex", params);
	}

	private void putParams(Map<String, Object> map, String[] params) {
		if (params == null) {
			throw new BizException(ErrorCode.PARAMS_IS_NULL);
		}
		if (params.length != 10) {
			throw new BizException(ErrorCode.PARAMS_NUM_ERROR);
		}
		try {
			if ( params[0] != null && !"".equals(params[0].trim()) ) {
				map.put("code", params[0].trim());
			}
			if ( params[1] != null && !"".equals(params[1].trim()) ) {
				map.put("name", params[1].trim());
			}
			if ( params[2] != null && !"".equals(params[2].trim()) ) {
				map.put("carModelId", Integer.parseInt(params[2].trim()));
			}
			if ( params[3] != null && !"".equals(params[3].trim()) ) {
				map.put("source", Integer.parseInt(params[3].trim()));
			}
			if ( params[4] != null && !"".equals(params[4].trim()) ) {
				map.put("compId", Integer.parseInt(params[4].trim()));
			}
		} catch (RuntimeException re) {
			if (re instanceof ClassCastException) {
				throw new BizException(ErrorCode.PARAMS_TYPE_ERROR);
			} else if (re instanceof NumberFormatException) {
				throw new BizException(ErrorCode.PARAMS_TYPE_ERROR);
			} else {
				throw new BizException(ErrorCode.PARAMS_OTHER_ERROR);
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PartProperty> getPartPropertyListByConditions(Map map) {
		return getSqlMapClientTemplate().queryForList(namespace+".getListByCondition",map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public PartProperty getPartPropertyByConditions(Map map) {
		List<PartProperty> list=this.getPartPropertyListByConditions(map);
		return list.size()>0?list.get(0):null;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<PartProperty> getPartPropertyByCompanyId(Map map) {
		
		return this.getSqlMapClientTemplate().queryForList(super.namespace+".getPartPropertyByCompanyId",map);
	}
	
	@Override
	public int countByConditions(PartListModel partPropertyListModel){
		
		return  (Integer)this.getSqlMapClientTemplate().queryForObject(namespace+".countByConditions",partPropertyListModel);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean deletePartProperty(Integer companyId,Integer id){
		
		Map map =new HashMap();
		
		map.put("companyId", companyId);
		
		map.put("id",id);
		
		return getSqlMapClientTemplate().delete(namespace + ".deletePartProperty", map) == 1;
		
	}

	@Override
	public void savePartProperty(PartProperty partProperty) {
		// TODO Auto-generated method stub
		getSqlMapClientTemplate().insert(namespace + ".create", partProperty);
	}

	@Override
	public boolean updatePartProperty(PartProperty partProperty) {
		// TODO Auto-generated method stub
		return getSqlMapClientTemplate().update(namespace + ".update", partProperty) == 1;
	}

	
	@Override
	public void batchUpdatePartProperty(final List<PartProperty> partProperties) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				int batch = 0;
				for(PartProperty partProperty : partProperties){
					executor.update(namespace+".update", partProperty);
					batch++;
					if(batch == 100){
						executor.executeBatch();
						batch=0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});
		
	}

	@Override
	public void batchInsertPartProperty(final List<PartProperty> partProperties) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				int batch =0;
				for(PartProperty partProperty : partProperties){
					executor.insert(namespace+".create", partProperty);
					batch++;
					if(batch == 100){
						executor.executeBatch();
						batch=0;
					}
				}
				executor.executeBatch();				
				return null;
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List queryPartProperty(PartListModel listModel) {
		
		 List PartPropertys = this.getSqlMapClientTemplate().queryForList(super.namespace+".getPartPropertyByConditions",listModel);
		 return PartPropertys;
	}
	@SuppressWarnings("unchecked")
	public List<PartProperty> getPartAndPartPropertyByCondition(Map map){
		return this.getSqlMapClientTemplate().queryForList(namespace+".getPartAndPartPropertyByCondition",map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PartProperty> lockPartPropertyByCompanyId(Integer companyId) {
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("companyId", companyId);
		map.put("isLock", true);
		return this.getSqlMapClientTemplate().queryForList(namespace+".getPartAndPartPropertyByCondition", map);
		
	}

	@SuppressWarnings("unchecked")
	public List<PartProperty> getListByCondition(Map<String, Object> map){
		return getSqlMapClientTemplate().queryForList(namespace+".getListByCondition",map);
	}
	
	public Integer getCountByCondition(Map<String, Object> map){
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace+".getCountByCondition",map);
	}
	public List<Map> getListMapByCondition(Map<String, Object> map){
		return getSqlMapClientTemplate().queryForList(namespace+".getListMapByCondition",map);
	}
	public PartProperty getByCondition(Map<String, Object> map){
		List<PartProperty> list=this.getListByCondition(map);
		return list.size()>0?list.get(0):null;
	}
	public List<Map> getCodeByCompanyIdWithPage(Map<String, Object> map){
		return getSqlMapClientTemplate().queryForList(namespace+".getCodeByCompanyIdWithPage",map);
	}
	
}
